package com.ys.data.auth.controller;

import com.common.base.BaseEnum;
import com.common.pojo.Result;
import com.common.vo.UserLoginVo;
import com.common.vo.UserRoleVo;
import com.ys.data.auth.service.UserService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(value = "用户相关接口API")
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;


    @ApiOperation(value = "登录",notes = "用户登录")
    @PostMapping("login")
    public Result<?> login(@RequestBody UserLoginVo userLoginVo) throws Exception {
        return userService.login(userLoginVo);
    }

    @ApiOperation(value = "添加用户",notes = "添加用户")
    @PostMapping("insert")
    public Result<?> saveUser(@RequestBody UserRoleVo userRoleVo) throws Exception {
        Result result = new Result();
        result.OK(userService.saveUser(userRoleVo));
        return result;
    }

    @ApiOperation(value = "更新用户",notes = "更新用户")
    @PostMapping("update")
    public Result<?> updateUser(@RequestBody UserRoleVo userRoleVo) throws Exception {
        Result result = new Result();
        result.OK(userService.updateUser(userRoleVo));
        return result;
    }

    @ApiOperation(value = "查询用户",notes = "查询用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
    @GetMapping("find")
    public Result<?> findUser(@RequestParam Long id) throws Exception {
        Result result = new Result();
        result.OK(userService.findUser(id));
        return result;
    }

    @ApiOperation(value = "删除用户",notes = "删除用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
    @DeleteMapping("delete")
    public Result<?> deleteUser(@RequestBody UserRoleVo userRoleVo, HttpServletRequest request) throws Exception {
        Result result = new Result();
        Claims claims=(Claims) request.getAttribute("admin_claims");
        if(claims==null){
            return new Result(BaseEnum.NO_AUTHORITY);
        }
        result.OK(userService.deleteUser(userRoleVo));
        return result;
    }
}
